package com.example.rabbitmq.listener;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author malty
 */
@Component
public class RabbitMqListener {
    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue1(String msg) {
        System.out.println("接收到消息：【" + msg + "】");
    }

    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue2(String msg) {
        System.err.println("接收到消息：【" + msg + "】");
    }


    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("接收到消息fanout1：【" + msg + "】");
    }

    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.err.println("接收到消息fanout2：【" + msg + "】");
    }


    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue("direct.queue1"),
            exchange = @Exchange("malty.direct"),
            key = {"blue", "red"})})
    public void listenDirectQueue1(String msg) {
        System.out.println("接收到消息direct1：【" + msg + "】");
    }

    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange("malty.direct"),
            key = {"blue", "yellow"})})
    public void listenDirectQueue2(String msg) {
        System.err.println("接收到消息direct2：【" + msg + "】");
    }

    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(value = "malty.topic", type = "topic"),
            key = {"#.news"})})
    public void listenTopicQueue1(String msg) {
        System.out.println("接收到消息direct1：【" + msg + "】");
    }

    /**
     * 工作队列案例
     *
     * @param msg
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue("topic.queue2"),
            exchange = @Exchange(name = "malty.topic", type = "topic"),
            key = {"china.#"})})
    public void listenTopicQueue2(String msg) {
        System.err.println("接收到消息direct2：【" + msg + "】");
    }
}
